﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Text;
using System.Drawing.Imaging;
/// <summary>
/// 文件上传系统
/// </summary>
public class UpLoadFile : System.Web.UI.Page
{
    public UpLoadFile()
    {
        //
        // TODO: 在此处添加构造函数逻辑
        //
    }

    /// <summary>
    /// 商品图片文件上传
    /// </summary>
    /// <param name="Files">PostedFile </param>
    /// <param name="FileName">文件名</param>
    /// <returns>返回上传后的新文件名</returns>
    public string UpLoadProImg(HttpPostedFile Files, string FileName)
    {
        string photoName1 = FileName; //获取初始文件名
        int i = photoName1.LastIndexOf("."); //取得文件名中最后一个"."的索引
        string newext = photoName1.Substring(i).ToLower(); //获取文件扩展名
        if (newext != ".gif" && newext != ".jpg" && newext != ".jpeg" && newext != ".bmp" && newext != ".png")
        {
            DB.Alert("上传图片格式不是jpg,jpeg,bmp,png,gif格式");
            return photoName1;
        }
        if (Files.ContentLength > 2048000)
        {
            DB.Alert("上传图片太大~！");

        }
        string proImg = DateTime.Now.ToString("yyyyMMddHHmmssfff") + Files.ContentLength.ToString() + newext; //重新为文件命名,时间毫秒部分+文件大小+扩展名
        Files.SaveAs(Server.MapPath("..\\ProductImages\\" + proImg)); // 保存文件到路径,用Server.MapPath()取当前文件的绝对目录.在asp.net里"\"必须用"\\"代替
        //验证图片
        StreamReader sr = new StreamReader(Server.MapPath("..\\ProductImages\\" + proImg), Encoding.Default);
        string strContent = sr.ReadToEnd();
        sr.Close();
        string str = " request |script |.getfolder|.createfolder|.deletefolder|.createdirectory|.deletedirectory|.saveas|wscript.shell|script.encode|server.|.createobject| execute | activexobject | language=";
        foreach (string s in str.Split('|'))
            if (strContent.IndexOf(s) != -1)
            {
                File.Delete(Server.MapPath("..\\ProductImages\\" + proImg));
                //Response.Write("<script >function window.onload() {alert('上传图片格式不对~!');}</script>");
                DB.Alert("上传图片有问题~！");
                break;
            }
        return proImg;
    }

    /// <summary>
    /// 商品图片文件上传
    /// </summary>
    /// <param name="Files">PostedFile </param>
    /// <param name="FileName">文件名</param>
    /// <returns>返回上传后的新文件名</returns>
    public string ModifyProImg(HttpPostedFile Files, string FileName,string oldFile)
    {
        string photoName1 = FileName; //获取初始文件名
        int i = photoName1.LastIndexOf("."); //取得文件名中最后一个"."的索引
        string newext = photoName1.Substring(i).ToLower(); //获取文件扩展名
        if (newext != ".gif" && newext != ".jpg" && newext != ".jpeg" && newext != ".bmp" && newext != ".png")
        {
            DB.Alert("上传图片格式不是jpg,jpeg,bmp,png,gif格式");
            return photoName1;
        }
        if (Files.ContentLength > 2048000)
        {
            DB.Alert("上传图片太大~！");

        }
        string proImg = oldFile;
        //string proImg = DateTime.Now.ToString("yyyyMMddHHmmssfff") + Files.ContentLength.ToString() + newext; //重新为文件命名,时间毫秒部分+文件大小+扩展名
        Files.SaveAs(Server.MapPath("..\\ProductImages\\" + proImg)); // 保存文件到路径,用Server.MapPath()取当前文件的绝对目录.在asp.net里"\"必须用"\\"代替
        //验证图片
        StreamReader sr = new StreamReader(Server.MapPath("..\\ProductImages\\" + proImg), Encoding.Default);
        string strContent = sr.ReadToEnd();
        sr.Close();
        string str = " request |script |.getfolder|.createfolder|.deletefolder|.createdirectory|.deletedirectory|.saveas|wscript.shell|script.encode|server.|.createobject| execute | activexobject | language=";
        foreach (string s in str.Split('|'))
            if (strContent.IndexOf(s) != -1)
            {
                File.Delete(Server.MapPath("..\\ProductImages\\" + proImg));
                //Response.Write("<script >function window.onload() {alert('上传图片格式不对~!');}</script>");
                DB.Alert("上传图片有问题~！");
                break;
            }
        return proImg;
    }

    /**/
    /// <summary>
    /// 缩小图片
    /// </summary>
    /// <param name="strOldPic">源图文件名(包括路径)</param>
    /// <param name="strNewPic">缩小后保存为文件名(包括路径)</param>
    /// <param name="intWidth">缩小至宽度</param>
    /// <param name="intHeight">缩小至高度</param>
    public void SmallPic(string strOldPic, string strNewPic, int intWidth, int intHeight)
    {
        System.Drawing.Bitmap objPic, objNewPic;
        try
        {
            objPic = new System.Drawing.Bitmap(strOldPic);
            objNewPic = new System.Drawing.Bitmap(objPic, intWidth, intHeight);
            objNewPic.Save(strNewPic);
        }
        catch (Exception exp)
        { throw exp; }
        finally
        {
            objPic = null;
            objNewPic = null;
        }
    }

    
/// <summary>
/// 图片文件上传
/// </summary>
/// <param name="Files">PostedFile </param>
/// <param name="FileName">文件名</param>
/// <returns>返回上传后的新文件名</returns>
    public string UpLoadFileImg(HttpPostedFile Files,string FileName)
    {
            string photoName1 = FileName; //获取初始文件名
            int i = photoName1.LastIndexOf("."); //取得文件名中最后一个"."的索引
            string newext = photoName1.Substring(i).ToLower(); //获取文件扩展名
            if (newext != ".gif" && newext != ".jpg" && newext != ".jpeg" && newext != ".bmp" && newext != ".png" )
            {
                DB.Alert("上传图片格式不是jpg,jpeg,bmp,png,gif格式");
                return photoName1;
            }
            if (Files.ContentLength > 2048000)
            {
                DB.Alert("上传图片太大~！");

            }
            string photoName2 = DateTime.Now.ToString("yyyyMMddHHmmssfff") + Files.ContentLength.ToString() + newext; //重新为文件命名,时间毫秒部分+文件大小+扩展名
            Files.SaveAs(Server.MapPath("..\\ProductImages\\" + photoName2)); // 保存文件到路径,用Server.MapPath()取当前文件的绝对目录.在asp.net里"\"必须用"\\"代替
           //验证图片
            StreamReader sr = new StreamReader(Server.MapPath("..\\ProductImages\\" + photoName2), Encoding.Default);
            string strContent = sr.ReadToEnd();
            sr.Close();
            string str = " request |script |.getfolder|.createfolder|.deletefolder|.createdirectory|.deletedirectory|.saveas|wscript.shell|script.encode|server.|.createobject| execute | activexobject | language=";
            foreach (string s in str.Split('|'))
                if (strContent.IndexOf(s) != -1)
                {
                    File.Delete(Server.MapPath("..\\ProductImages\\" + photoName2));
                    //Response.Write("<script >function window.onload() {alert('上传图片格式不对~!');}</script>");
                    DB.Alert("上传图片有问题~！");
                    break;
                }
            return photoName2;
    }
    /// <summary>
    /// 压缩类（zip，rar）文件上传
    /// </summary>
    /// <param name="Files">PostedFile </param>
    /// <param name="FileName">文件名</param>
    /// <returns>返回上传后的新文件名</returns>
    public string UpLoadFileZip(HttpPostedFile Files, string FileName)
    {
        string ZipName1 = FileName; //获取初始文件名
        int i = ZipName1.LastIndexOf("."); //取得文件名中最后一个"."的索引
        string newext1 = ZipName1.Substring(i).ToLower(); //获取文件扩展名
        if (newext1 != ".rar" && newext1 != ".zip")
        {
            DB.Alert("上传文件格式不是rar或zip格式");
            return ZipName1;
        }
        if (Files.ContentLength > 12048000)
        {
            DB.Alert("上传图片太大~！");
            return ZipName1;

        }
        string ZipName2 = DateTime.Now.ToString("yyyyMMddHHmmssfff") + Files.ContentLength.ToString() + newext1; //重新为文件命名,时间毫秒部分+文件大小+扩展名
        Files.SaveAs(Server.MapPath("..\\ProductImages\\" + ZipName2)); // 保存文件到路径,用Server.MapPath()取当前文件的绝对目录.在asp.net里"\"必须用"\\"代替

        return ZipName2;
    }
}

